<template>
  <div>
    <div class="add_table_css">
      <div class="explain_css">
        <p>出版著作、论文要填写书名、书号或刊号、出版社名称、卷/期数、栏目、页码等；</p>
        <p>合著要填写自己的名次和独立完成数量；</p>
        <p>在学术会议上交流的文章要注明学术会议名称、主办单位、文章交流的层次及获奖情况。</p>
      </div>
      <el-button plain @click="addEducation" class="btn_css_newadd">新增著作论文</el-button>
      <!-- 新增著作论文的弹框 -->
      <el-dialog title="著作论文详细" :visible.sync="dialogFormVisible" width="40%" center>
        <el-form
          label-width="200px"
          :model="paperTable"
          :rules="rules"
          ref="paperTable"
          class="demo-ruleForm add_education_css"
        >
          <el-form-item label="论文著作会议题目：" prop="theme">
            <el-input v-model="paperTable.theme"></el-input>
          </el-form-item>
          <el-form-item label="发表/出版/会议日期：" prop="created">
            <el-date-picker
              v-model="paperTable.created"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="timestamp"
            ></el-date-picker>
            <!-- <el-date-picker type="date" placeholder="1987-09-23" v-model="paperTable.created"></el-date-picker> -->
          </el-form-item>
          <el-form-item label="刊物/出版社/会议名称：" prop="publish">
            <el-input v-model="paperTable.publish"></el-input>
          </el-form-item>
          <el-form-item label="期刊号/书号：" prop="magazine">
            <el-input v-model="paperTable.magazine"></el-input>
          </el-form-item>
          <el-form-item label="内容提要：" prop="informativeAbstract">
            <el-input type="textarea" v-model="paperTable.informativeAbstract"></el-input>
          </el-form-item>
          <el-form-item label="出版、登载、学术会议交流及获奖情况：" prop="awards">
            <el-input type="textarea" v-model="paperTable.awards"></el-input>
          </el-form-item>
          <el-form-item label="合（独）著、译著：" prop="coAuthoredAndTranslated">
            <el-input type="textarea" v-model="paperTable.coAuthoredAndTranslated"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer add_footer_btn_css">
          <el-button plain @click="add('paperTable')" class="btn_css_bord">保存</el-button>
          <el-button plain @click="dialogFormVisible = false" class="btn_css_bord">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 表格 -->
      <div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="50"></el-table-column>
          <el-table-column prop="theme" label="论文著作会议题目" width="220"></el-table-column>
          <el-table-column prop="created" label="发表/出版/会议日期" width="150"></el-table-column>
          <el-table-column prop="publish" label="刊物/出版社/会议名称" width="200"></el-table-column>
          <el-table-column prop="magazine" label="期刊/书号" width="100"></el-table-column>
          <el-table-column label="操作" width="220">
            <template slot-scope="scope">
              <div class="btnDiv1_css">
                <el-button
                  @click.native.prevent="update(scope.$index, tableData)"
                  plain
                  size="small"
                  class="btn_css_er"
                >修改</el-button>
                <el-button
                  @click.native.prevent="del(scope.$index, tableData)"
                  plain
                  size="small"
                  class="btn_css_er"
                >删除</el-button>
              </div>
              <div>
                <el-button
                  @click.native.prevent="del2(scope.$index, tableData)"
                  plain
                  size="small"
                  class="btn_css_si"
                >上传附件</el-button>
                <el-button
                  @click.native.prevent="del3(scope.$index, tableData)"
                  plain
                  size="small"
                  class="btn_css_si"
                >查看附件</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 修改著作论文的弹框 -->
      <el-dialog title="修改著作论文" :visible.sync="dialogFormVisible2" width="40%" center>
        <el-form
          label-width="200px"
          :model="paperTable2"
          :rules="rules"
          ref="paperTable2"
          class="demo-ruleForm add_education_css"
        >
          <el-form-item label="论文著作会议题目：" prop="theme">
            <el-input v-model="paperTable2.theme"></el-input>
          </el-form-item>
          <el-form-item label="发表/出版/会议日期：" prop="created">
            <el-date-picker type="date" v-model="paperTable2.created"></el-date-picker>
          </el-form-item>
          <el-form-item label="刊物/出版社/会议名称：" prop="publish">
            <el-input v-model="paperTable2.publish"></el-input>
          </el-form-item>
          <el-form-item label="期刊号/书号：" prop="magazine">
            <el-input v-model="paperTable2.magazine"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer add_footer_btn_css">
          <el-button plain @click="updateOk('paperTable2')" class="btn_css_bord">保存</el-button>
          <el-button plain @click="dialogFormVisible2 = false" class="btn_css_bord">关闭</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 分页 -->
    <div class="block page_css">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="limit"
        @current-change="change"
        @prev-click="prev"
        @next-click="next"
      ></el-pagination>
    </div>
    <el-button @click="declare" plain class="btn_css">确定申报</el-button>
  </div>
</template>
<script>
/* 引入时间戳转换成日期函数 */
import "../../../../utils/date";

/* 引入日期转换成时间戳函数 */
// import publicMethod from "../../../../utils/time";
//使用

// publicMethod.getTimestamp('2019-08-27 00:03:04')

export default {
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      paperTable: {
        theme: "",
        created: "",
        publish: "",
        magazine: "",
        informativeAbstract: "",
        awards: "",
        coAuthoredAndTranslated: ""
      },
      paperTable2: {},
      rules: {
        theme: [{ required: true, message: "请输入毕业院校", trigger: "blur" }],
        created: [
          { required: true, message: "请输入毕业院校", trigger: "change" }
        ],
        publish: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        magazine: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        informativeAbstract: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        awards: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        coAuthoredAndTranslated: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    /* 渲染页面 */
    getCon() {
      this.$axios({
        url: "/article/list",
        params: {
          page: this.page,
          limit: this.limit
        }
      }).then(res => {
        // console.log(res.data);
        // this.
        this.tableData = res.data.data;
        this.total = res.data.count;
      });
    },
    /* 分页渲染 */
    change(page) {
      this.page = page;
      this.getCon();
    },
    prev() {
      this.page--;
      this.getCon();
    },
    next() {
      this.page++;
      this.getCon();
    },

    /* 新增弹出框 */
    addEducation() {
      this.dialogFormVisible = true;
      // this.paperTable = {...(res.data.data)}
    },
    /* 新增 */
    add(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   alert("submit!");
          this.$axios({
            url: "/article/add",
            method: "post",
            data: {
              theme: this.paperTable.theme,
              created: this.paperTable.created,
              publish: this.paperTable.publish,
              magazine: this.paperTable.magazine,
              informativeAbstract: this.paperTable.informativeAbstract,
              awards: this.paperTable.awards,
              coAuthoredAndTranslated: this.paperTable.coAuthoredAndTranslated
            }
          }).then(res => {
            // console.log(res);
            if (res.status == 200) {
              this.$message.success("添加成功！");
              this.getCon();
            } else {
              this.$message.error("添加失败！");
            }
          });
          this.dialogFormVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    /* 修改弹出框 */
    update(index, data) {
      this.dialogFormVisible2 = true;
      console.log(index, data);
      this.paperTable2 = { ...data[index] };
    },
    /* 修改渲染 */
    updateOk(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios({
            url: "/article/update",
            method: "post",
            data: {
              id: this.paperTable2.id,
              theme: this.paperTable2.theme,
              created: this.paperTable2.created,
              publish: this.paperTable2.publish,
              magazine: this.paperTable2.magazine
            }
          }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success("修改成功！");
              this.getCon();
            } else {
              this.$message.error("修改失败！");
            }
          });
          this.dialogFormVisible2 = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* 删除渲染 */
    del(index, data) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios({
            url: "/article/delete",
            method: "post",
            data: {
              id: data[index].id
            }
          }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success("删除成功！");
              this.getCon();
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {
          this.$message.info("已取消删除！");
        });
    },

    handleClick(row) {
      this.dialogFormVisible2 = true;
      console.log(row);
    },

    del2() {},
    del3() {},

    declare() {
      this.$router.push("/personalinformation/jobtitledeclare");
    }
  },
  /* 挂载时就渲染 */
  mounted() {
    this.getCon();
    sessionStorage.setItem("fName", 'Process9');
  }
  // mounted() {
  //   sessionStorage.setItem("activeIndex", 1);
  // }

};
</script>
<style scoped lang="less">
.btnDiv1_css {
  margin-bottom: 6px;
  .el-button:first-child {
    margin-right: 20px;
  }
}
.page_css {
  margin: 10px;
  text-align: right;
}
.el-table {
  font-size: 13px;
}
.btn_css_newadd {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  text-align: center;
}
.btn_css_er {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 60px;
  height: 30px;
  text-align: center;
}
.btn_css_si {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 80px;
  height: 30px;
  text-align: center;
}
.btn_css_bord {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 80px;
  height: 40px;
  font-size: 14px;
  text-align: center;
}
.btn_css {
  background-color: #f5f7fa;
  border: 1px solid #eee;
  width: 150px;
  height: 40px;
  display: block;
  font-size: 14px;
  margin: 20px auto;
}
.explain_css {
  margin-bottom: 20px;
  p {
    color: red;
    font-size: 14px;
  }
}
.btn_css {
  width: 150px;
  height: 40px;
  display: block;
  margin: 20px auto;
}
.add_education_css {
  width: 420px;
  margin-left: 40px;
}
.add_footer_btn_css {
  margin: 0 auto;
}
.add_table_css {
  border: 1px solid #eee;
  // height: 400px;
  padding: 20px;
}
</style>